/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "validator.h"
#include "lookuplineedit.h"
#include "query.h"
#include "kontamabilloader.h"
#include "addwidetinterface.h"

using namespace SQL;

Validator::Validator(QString listQuery, QString listField, bool sortedList, QString appendPluginName, QObject *parent)
 : QValidator(parent),m_field(listField),m_sorted(sortedList),m_addPlugin(0)
{
	m_query=defaultDatabase()->query(listQuery);
	setAppendPluginName(appendPluginName);
}

void Validator::setAppendPluginName(const QString & appendPluginName)
{
	m_addPlugin=0;
	foreach(KontAMabil::MenuWidgetInterface* menu, kontAMabil()->menuPlugins())
		if (dynamic_cast<KontAMabil::AddWidetInterface*>(menu) && dynamic_cast<KontAMabil::AddWidetInterface*>(menu)->name().toLower().trimmed()==appendPluginName.toLower().trimmed())
		{
			m_addPlugin=dynamic_cast<KontAMabil::AddWidetInterface*>(menu);
			break;
		}
}

/*
QWidget * Validator::rootParent() const
{
	QWidget * lastObject=dynamic_cast<QWidget*>(parent());
	if (!lastObject)
		return false;
	while(lastObject->parentWidget())
		lastObject=lastObject->parentWidget();
	return lastObject;
}
*/

QValidator::State Validator::validate ( QString & input, int & pos ) const
{
	if (!m_mutex.tryLock())
		return QValidator::Intermediate;

	Q_UNUSED(pos);
	QValidator::State state=QValidator::Invalid;

	if (!m_query || m_query->state()!=Query::Select)
	{
		m_mutex.unlock();
		return state;
	}
	state=(m_query->locate(m_field, input, false, false, m_sorted))?QValidator::Acceptable:(m_query->locate(m_field, input, false, true, m_sorted))?QValidator::Intermediate:QValidator::Invalid;
	if (QValidator::Invalid==state && m_addPlugin)
	{
		QString result;
		qint64 id;
		if (!m_addPlugin->add(0/*rootParent()*/, input, &result, & id))
		{
			m_mutex.unlock();
			return state;
		}

		if (m_query->value(m_field)==result)
			state=QValidator::Acceptable;
		else
			state=m_query->locate(m_field, input, false, false, m_sorted)?QValidator::Acceptable:m_query->locate(m_field, input, false, true, m_sorted)?QValidator::Intermediate:QValidator::Invalid;

		if (QValidator::Invalid==state)
		{
			m_query->select();
			state=m_query->locate(m_field, input, false, false, m_sorted)?QValidator::Acceptable:m_query->locate(m_field, input, false, true, m_sorted)?QValidator::Intermediate:QValidator::Invalid;
		}
		if (QValidator::Acceptable==state)
			input=result;
	}
	m_mutex.unlock();
	return state;
}

